#region Copyright (c) Nicola Gallo All rights reserved.
/*
 * All rights reserved. Reproduction of trasmission of this file, or a portion thereof,
 * is forbidden without prior written permission of Nicola Gallo.
 * 
 * Author: Nicola Gallo
 * Date: 09/04/2011
 * 
 * */
#endregion
namespace PrismMVC.ModuleA.ViewModels
{
    //.NET References
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Data;

    //Microsoft References
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Prism.Logging;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.Prism.ViewModel;
    using Microsoft.Practices.Unity;

    //PrismMVC References
    using PrismMVC.ModuleA.Data;
    using PrismMVC.ModuleA.Services;
    using PrismMVC.Shell.Containers;

    /// <summary>
    /// PresentationModel for Persons.
    /// </summary>
    public class PersonsPM : PresentationModel
    {
        //Constructors
        
        /// <summary>
        /// Creates a new instance of <see cref="PersonsPM"/>.
        /// </summary>
        /// <param name="unityContainer">Unity container.</param>
        /// <param name="loggerFacade">Logger facade.</param>
        /// <param name="eventAggregator">Event aggregator.</param>
        /// <param name="personsService">Persons service.</param>
        public PersonsPM(IUnityContainer unityContainer, ILoggerFacade loggerFacade, IEventAggregator eventAggregator, IPersonsService personService)
            : base(unityContainer, loggerFacade, eventAggregator)
        {
            if (personService == null)
            {
                throw new ArgumentNullException("PersonsService is equals to null.");
            }
            this.ShowPersonCommand = new DelegateCommand<Object>(this.ExecuteShowPersonCommand, this.CanExecuteShowPersonCommand);
            this.Persons = new ObservableCollection<Person>(personService.GetAllPersons());
        }

        //Properties

        /// <summary>
        /// Gets persons.
        /// </summary>
        public ObservableCollection<Person> Persons { get; private set; }

        /// <summary>
        /// Show person command.
        /// </summary>
        public DelegateCommand<Object> ShowPersonCommand { get; private set; }

        //Methods

        /// <summary>
        /// Return true if it is possible to execute the ShowPerson command, false otherwise.
        /// </summary>
        /// <param name="commandParamter">Command parameter</param>
        /// <returns>True if it is possible to execute the command, false otherwise.</returns>
        private bool CanExecuteShowPersonCommand(Object commandParamter)
        {
            return true;
        }

        /// <summary>
        /// Execute ShowPerson command.
        /// </summary>
        /// <param name="person">Person.</param>
        private void ExecuteShowPersonCommand(Object person)
        {
            if (person is Person)
            {
                this.InvokeAction("ShowPerson", new { person = person as Person });    
            }            
        }
    }
}